package com.xyc.gulimall.order.listener;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.rabbitmq.client.Channel;
import com.xyc.gulimall.order.config.AlipayTemplate;
import com.xyc.gulimall.order.entity.OrderEntity;
import com.xyc.gulimall.order.service.OrderService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RabbitListener(queues = "order.release.order.queue")
public class OrderCloseListener {

    @Autowired
    private OrderService orderService;

    @Autowired
    private AlipayTemplate alipayTemplate;

    @Autowired
    private HttpServletRequest request;

    @RabbitHandler
    public void listener(OrderEntity orderEntity, Message message, Channel channel) throws IOException {
        System.out.println("收到过期的订单信息，准备关闭订单：" + orderEntity.toString());
        try {
            orderService.closeOrder(orderEntity);
            //手动调用支付宝收单
            manualOrder(orderEntity.getOrderSn());
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }

    private void manualOrder(String orderSn) throws AlipayApiException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                alipayTemplate.getGatewayUrl(),
                alipayTemplate.getApp_id(),
                alipayTemplate.getMerchant_private_key(),
                "json",
                alipayTemplate.getCharset(),
                alipayTemplate.getAlipay_public_key(),
                alipayTemplate.getSign_type()
        );

        //设置请求参数
        AlipayTradeCloseRequest alipayRequest = new AlipayTradeCloseRequest();
        //商户订单号，商户网站订单系统中唯一订单号
//        String out_trade_no = new String(request.getParameter("WIDTCout_trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
//        System.out.println("out_trade_no:" + out_trade_no);

        //支付宝交易号
//        String trade_no = new String(request.getParameter("WIDTCtrade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
//        System.out.println("trade_no:" + trade_no);
        //请二选一设置

//        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"trade_no\":\"" + trade_no + "\"}");
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + orderSn + "\"}");

        //请求
        String result = alipayClient.execute(alipayRequest).getBody();

        //输出
        System.out.println(result);
    }
}
